//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
// Portions Copyright (c) MiTAC Corp. All rights reserved.
//
// File:    usbEboot.c
// Programmer:   Visual.Wei
// Date: 2007-4
// Description: Eboot may use those functions to download image
//
#include <windows.h>
#include <oal_kitl.h>
#include <oal_log.h>
#include <blcommon.h>
#include "sermddapi.h"

static KITL_SERIAL_INFO g_serialInfo;
static OAL_KITL_SERIAL_DRIVER * g_usb;
extern OAL_KITL_SERIAL_DRIVER g_kitlUsbSerial;
//---------------------------------------------------------------------------------

const OAL_KITL_SERIAL_DRIVER *GetKitlUSBSerialDriver (void)
{
	return &g_kitlUsbSerial;
}

UINT32 OEMUSBInit()
{
	UINT32 rc = BL_ERROR;

	// Get device driver
	g_usb=&g_kitlUsbSerial;

	memset(&g_serialInfo, 0, sizeof(g_serialInfo));

	// Init
	if (!g_usb->pfnInit(&g_serialInfo))
	{
		OALMSG(OAL_ERROR, (L"ERROR: device driver Init call failed\r\n"));
		goto cleanUp;
	}

	rc = BL_DOWNLOAD;
	OALMSG(1, (L"end\r\n"));

cleanUp:
	return rc;
}

void OEMUSBDeinit()
{
	g_usb->pfnDeinit();
}

BOOL OEMUSBReadData(ULONG size, UCHAR *pData)
{
	volatile ULONG left;
	UCHAR *buf;
	
	if(!g_usb)
		return FALSE;
	
	left=size;
	buf=pData;
	while(left>0)
	{
		if(left>(1<<15))
		{
			g_usb->pfnRecv(buf,1<<15 );
			buf+=(1<<15);
			left-=(1<<15);
		}
		else
		{
			g_usb->pfnRecv(buf,(UINT16)left);
			left=0;
		}			
			
	}
	return TRUE;
}
//
BOOL OEMUSBWriteData(ULONG size, UCHAR *pData)
{
	ULONG left;
	UCHAR *buf;
	
	if(!g_usb)
		return FALSE;
	
	left=size;
	buf=pData;
	while(left>0)
	{
		if(left>(1<<15))
		{
			g_usb->pfnSend(buf,1<<15 );
			buf+=(1<<15);
			left-=(1<<15);
		}
		else
		{
			g_usb->pfnSend(buf,(UINT16)left);
			left=0;
		}			
			
	}
	return TRUE;
}

